<template>
  <div id="charge">
    <div class="c-box">
      <div>收费管理/收费项目</div>
      <h3>收费项目管理</h3>
      <div class="border">
        <span>收费方式</span>
        <el-select v-model="method" class="m-2" placeholder="请选择">
          <el-option
            v-for="item in feeType"
            :key="item.id"
            :value="item.method"
          />
        </el-select>
        <span class="project"
          >项目名称
          <el-input v-model="input" placeholder="请输入" />
        </span>
        <el-button type="primary" @click="getData()">查询</el-button>
        <el-button @click="reset()">重置</el-button>
      </div>
    </div>

    <div class="c-box2">
      <div class="button">
        <p>项目列表</p>
        <div>
          <el-button type="primary">+&nbsp;&nbsp;新建</el-button>
          <el-button @click="delAll()">批量操作</el-button>
        </div>
      </div>

      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column type="selection" prop="id" width="55" />
        <el-table-column prop="service" label="收费项目" width="120" />
        <el-table-column prop="method" label="计费方式" width="120" />
        <el-table-column prop="price" label="单价" width="120" />
        <el-table-column prop="accuracy" label="精确度" width="120" />
        <el-table-column prop="formula" label="计算公式" width="160" />
        <el-table-column prop="lateFee" label="滞纳金比例" width="120" />
        <el-table-column prop="remarks" label="状态" width="120">
          <template #default="scope">
            <el-switch v-model="scope.row.remarks" />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" @click="handleEdit()">编辑</el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="footer">
        <Page
          :total="total"
          :count="count"
          :currentPage="page"
          @handleCurrentChange="handleCurrentChange"
          @handleSizeChange="handleSizeChange"
        ></Page>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
#charge {
  background: #f0f2f5;
  height: 100%;
  // width: 100%;
  h3 {
    margin: 20px 0;
  }
  .c-box {
    height: 150px;
    width: 90%; /*  */
    padding: 15px 0 15px 30px;
    margin: 20px auto;

    border: 1px solid #e9e9e9;
    background-color: #fff;
    .border {
      height: 50px;
      width: 100%;
      border-top: 1px solid #e9e9e9;
      padding-top: 20px;
      .list {
        // width: 320px;
        border: 1px solid #e9e9e9;
        padding: 8px 120px 8px 5px;
        margin: -2px 0 0 10px;
      }
      .el-icon--right {
        position: absolute;
        top: 6px;
        right: 3px;
      }
      .el-input {
        width: 280px;
        margin: -3px 3px 3px 6px;
      }
      .el-button {
        margin: -3px 3px 3px 6px;
        position: relative;
        top: 0;
        right: -150px;
        border-radius: 2px;
        width: 80px;
      }
      .project {
        margin-left: 140px;
      }
    }
  }
  .c-box2 {
    width: 90%; /*  */
    padding: 15px 0 15px 30px;
    margin: 20px auto;
    border: 1px solid #e9e9e9;
    background-color: #fff;
    .footer {
      width: 100%;
      height: 40px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
    .button {
      display: flex;
      justify-content: space-between;
      margin-right: 30px;
      .el-button {
        width: 95px;
        border-radius: 2px;
      }
    }
    .el-table {
      margin: 30px 0;
      .el-button {
        border: 0;
        background: white;
        color: #1890ff;
      }
    }
  }
}
</style>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import axios from '../../utils/http';
import { ElMessage } from 'element-plus';
import Page from '../../components/Page.vue';

const input = ref('');
const page = ref(1);
const count = ref(5);
const total = ref(0);
const tableData = ref([]);
const feeType = ref([{ id: 0, method: '' }]);
const method = ref('');

const handleEdit = () => {};
const handleDelete = (row: { code: number }) => {
  axios({
    url: '/bill/addNo',
    method: 'get',
  }).then(({ data }) => {
    if (data.code == '0') {
      getData();
      ElMessage.success('删除成功！');
    } else {
      ElMessage.error('删除失败！');
    }
  });
};
const delAll = () => {
  axios({
    url: '/payItem/ids/delete',
    method: 'delete',
    data: {
      ids: [],
    },
  }).then(({ data }) => {
    if (data.code == '0') {
      ElMessage.success('删除成功！');
      getData();
    } else {
      ElMessage.error('删除失败');
    }
  });
};
const list = () => {
  axios({
    url: '/method/queryMethodIdByName',
    method: 'get',
  }).then(({ data }) => {
    if (data.code == '0') {
      feeType.value = data.data;
    } else {
      ElMessage.error('加载失败');
    }
  });
};

const getData = () => {
  axios({
    url: '/payItem/list',
    method: 'get',
    params: {
      page: page.value,
      limit: count.value,
      method: method.value,
      service: input.value,
    },
  }).then(({ data }) => {
    if (data.code == '0') {
      tableData.value = data.data;
      total.value = data.total;
    } else {
      ElMessage.error('加载失败！');
    }
  });
};
const handleSizeChange = (val) => {
  count.value = val;
  getData();
};
const handleCurrentChange = (val) => {
  page.value = val;
  getData();
};
const reset = () => {
  input.value = '';
  method.value = '';
  getData();
};
onMounted(() => {
  getData();
  list();
});
</script>
